package me.exchange.com.modules.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.exchange.com.aop.log.Log;
import me.exchange.com.modules.system.domain.Dict;
import me.exchange.com.modules.system.dto.DictDto;
import me.exchange.com.modules.system.dto.DictQueryCriteria;
import me.exchange.com.modules.system.service.DictService;
import me.exchange.com.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* @author 张玉丰
* @date 2020-04-11
*/
@Api(tags = "数据字典管理")
@RestController
@RequestMapping("/api/dict")
public class DictController {
    @Autowired
    private DictService dictService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @PostMapping(value = "/list/download")
    @PreAuthorize("@el.check('dict:list')")
    public void download(HttpServletResponse response, DictQueryCriteria criteria) throws IOException {
        dictService.download(dictService.queryAll(criteria), response);
    }

    @PostMapping("/list")
    @Log("查询数据字典")
    @ApiOperation("查询数据字典")
    @PreAuthorize("@el.check('dict:list')")
    public ResponseEntity<Object> getDicts(DictQueryCriteria criteria, Pageable pageable){
        if(criteria.getCAdminTime() != null && criteria.getCAdminTime().size()>0){
            if(criteria.getCAdminTime().get(1).before(criteria.getCAdminTime().get(0))){
                return new ResponseEntity<>(Result.error("操作失败，查询时间段设置不正确"),HttpStatus.OK);
            }
        }
        List<DictDto> dict =  dictService.queryAll(criteria);
        return new ResponseEntity<>(Result.ok("数据字典查询成功").put("dictList",dict),HttpStatus.OK);
    }
    @PostMapping("/queryInfo")
    @Log("查询字典详情")
    @ApiOperation("查询字典详情")
    @PreAuthorize("@el.check('dict:list')")
    public ResponseEntity<Object> queryInfo(Long id){
        DictDto dictDto =  dictService.findById(id);
        return new ResponseEntity<>(Result.ok("字典详情查询成功").put("dict",dictDto),HttpStatus.OK);
    }

    @PostMapping("/add")
    @Log(value="新增数据字典",code="system_dict")
    @ApiOperation("新增数据字典")
    @PreAuthorize("@el.check('dict:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Dict resources){
        dictService.create(resources);
        return new ResponseEntity<>(Result.ok("数据字典新增成功"),HttpStatus.OK);
    }

    @PostMapping("/update")
    @Log(value="修改数据字典",code="system_dict")
    @ApiOperation("修改数据字典")
    @PreAuthorize("@el.check('dict:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Dict resources){
        dictService.update(resources);
        return new ResponseEntity<>(Result.ok("数据字典修改成功"),HttpStatus.OK);
    }

    @Log(value="删除数据字典",code="system_dict")
    @ApiOperation("删除数据字典")
    @PreAuthorize("@el.check('dict:del')")
    @PostMapping("/del")
    public ResponseEntity<Object> delete(@RequestBody DictQueryCriteria criteria) {
        dictService.deleteAll(criteria.getIds());
        return new ResponseEntity<>(Result.ok("数据字典删除成功"),HttpStatus.OK);
    }

}